<?php
/*
	新版系统支付接口
	新版支付接口的微信支付、支付宝支付等有异步结果回调通知的支付方式，此处支付完成后不再更改支付状态，待收到异步支付结果通知后再修改支付状态。
	新增付款码支付方式，付款码支付获取订单支付进行，进入支付页面，发起支付请求，完成支付，提示支付成功！然后呢？


*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'pay';
$return=array();
$setting=uni_setting('pay_en');
set_html('title','The payment page');
if($_RQ['op']=='copy'){
    if($_SESSION['container']=='wechat'){
        $id=intval($_RQ['id']);
        $pay_url=custom_url('core/member.pay/copy/id='.$id,true);
        $return['pay_url']=$pay_url;
        exi($return);
    }else{
        $_RQ['op']='pay';
    }
}
if($_RQ['op']=='pay'){
    $id=intval($_RQ['id']);
    if(!$id){
        exi('Initiate payment failed! Please select the order to pay first！','error');
    }
    $pay=pdo_get('core_paylog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
    if(!$pay){
        exi("Can't find the pay of the order！",'error');
    }elseif($pay['no_type']){
        $pay['no_type']=json_decode($pay['no_type'],true);
    }
    if($pay['status']){
        exi('The order was paid.','error');
    }
    $return=array(
        'id'=>$pay['id'],
        'tid'=>$pay['tid'],
        'fee'=>$pay['fee'],
    );
	$pay_types=array();
    if($_SESSION['uid'] && $setting['credit']){
        $pay_types['credit']=array('name'=>'credit','title'=>'Balance payment');
    }
    if($pay['no_type']){
		unset($pay_types[$pay['no_type']]);
    }
    $jy_paypal = check_account_permission('jy_paypal');
	if($jy_paypal){
		$jy_paypal=plugin('jy_paypal')->plugin_setting('basic');
		if($jy_paypal['isopen']){
			$pay_types['jy_paypal']=array('name'=>'jy_paypal','title'=>$jy_paypal['title']?:'PayPal Pay');
		}
	}
	$jy_stripe = check_account_permission('jy_stripe');
	if($jy_stripe){
		$jy_stripe=plugin('jy_stripe')->plugin_setting('basic');
		if($jy_stripe['isopen']){
			$pay_types['jy_stripe']=array('name'=>'jy_stripe','title'=>$jy_stripe['title']?:'Stripe Pay');
		}
	}
	$jy_asiabill = check_account_permission('jy_asiabill');
	if($jy_asiabill){
		$jy_asiabill=plugin('jy_asiabill')->plugin_setting('basic');
		if($jy_asiabill['isopen']){
			$pay_types['jy_asiabill']=array('name'=>'jy_asiabill','title'=>$jy_asiabill['title']?:'AsiaBill Pay');
		}
	}
	$jy_pingpong = check_account_permission('jy_pingpong');
	if($jy_pingpong){
		$jy_pingpong=plugin('jy_pingpong')->plugin_setting('basic');
		if($jy_pingpong['isopen']){
			$pay_types['jy_pingpong']=array('name'=>'jy_pingpong','title'=>$jy_pingpong['title']?:'PingPong Pay');
		}
	}
	$jy_onerway = check_account_permission('jy_onerway');
	if($jy_onerway){
		$jy_onerway=plugin('jy_onerway')->plugin_setting('basic');
		if($jy_onerway['isopen']){
			$pay_types['jy_onerway']=array('name'=>'jy_onerway','title'=>$jy_onerway['title']?:'Onerway Pay');
		}
	}
	$jy_wintopay = check_account_permission('jy_wintopay');
	if($jy_wintopay){
		$jy_wintopay=plugin('jy_wintopay')->plugin_setting('basic');
		if($jy_wintopay['isopen']){
			$pay_types['jy_wintopay']=array('name'=>'jy_wintopay','title'=>$jy_wintopay['title']?:'WinToPay Pay');
		}
	}
	if($setting['hdfk']){
		$pay_types['hdfk']=array('name'=>'hdfk','title'=>'Cash on delivery');
	}
    if($pay['pay_type']){
        if($pay_types[$pay['pay_type']]){
            $return['pay_type']=$pay['pay_type'];
        }else{
            if($pay['action']='member.recharge' && $setting['credit_wechat']){
                $return['pay_type']=$pay['pay_type'];
            }else{
                exi('No payment method available, please contact the system administrator！','error');
            }
        }
    }
	$return['pay_types']=array_values($pay_types);
	$return['type']=$return['pay_type']?:$return['pay_types'][0]['name'];
    if(!$return['pay_types'] && !$return['pay_type']){
        exi('No payment method available, please contact the system administrator！','error');
    }
    if($_RQ['forward_url']){
        $return['forward_url']=base64_decode($_RQ['forward_url']);
    }
    $return['di_open']=0;
    $return['api']=$setting['api'];
    exi($return);
}
if($_RQ['op']=='apipay'){
    $id=intval($_RQ['id']);
    if(!$id){
        exi('Parameter error！','error');
    }
	$pay_type=$_RQ['pay_type'];
    $di_open=$_RQ['di_open'];
    $pay_log=pdo_get('core_paylog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
    if($di_open){
        $difee=cfc('pay')->up_di_open($di_open,$pay_log);
        $pay_log['fee']=$pay_log['fee']-$difee;
    }else{
        //不抵扣
        pdo_update('core_paylog',array('di_credit'=>0,'di_fee'=>0),array('id'=>$pay_log['id']));
    }
    if($pay_log['fee']>0){
        $params=array(
            'title'=>$pay_log['title'],
            'fee'=>$pay_log['fee'],
            'ordersn'=>$pay_log['tid'],
            'return_url'=>custom_url($pay_log['success_url'],true),
        );
        $pay_html=model($pay_type.'/model')->pay($params);
        exi(base64_encode($pay_html));
    }else{
        cfc('pay')->upcredit_notify($pay_log,$di_open,$difee);
        exi('success');
    }
}
